package PreparedStatement;

import entity.Sys;
import util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Jason
 * @PACKAGE_NAME AddDeleteCheckAndCorrect
 * @Description 修改数据
 * @date 2023-12-06 14:01
 */
public class UpdateTest {
     public static int update(Sys sys) throws SQLException, ClassNotFoundException {
        String sql =  "update sys set username = ?, password = ? where id = ?";
        DbUtil dbUtil = new DbUtil();
        Connection conn = dbUtil.getConnection();
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,sys.getUsername());
        pst.setString(2,sys.getPassword());
        pst.setInt(3,sys.getId());
        int execute = pst.executeUpdate();
        System.out.println("操作了"+execute+"条数据");
        /*关闭数据库*/
        dbUtil.closeStatement(pst);
        dbUtil.closeConnection(conn);
        return execute;
    }

    public static void main(String[] args) {
        try {
            Sys sys = new Sys(146,"小明01","000000");
            int delete = UpdateTest.update(sys);
            if(delete != 0){
                System.out.println("修改成功");
            }
            else{
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
